#include "ComboSystem.h"
#include <fstream>
using std::ifstream;

// GetInstance
// Returns a static instance of the class to gain access to all the functions
ComboSystem* ComboSystem::GetInstance( void )
{
	static ComboSystem combo;
	return &combo;
}
// InitComboSystem
// Loads in all the combos that the player can perform if their level is high enough.
void ComboSystem::InitComboSystem( void )
{
	char weapon[ 10 ];
	char name[ 40 ];
	char buttons[ 10 ];
	char active[ 2 ];
	char damage[ 4 ];
	int nDamage = 0;

	ifstream is;
	is.open("Resources/Data Files/Combos.dat");
	if( is.is_open() )
	{
		while( !is.eof() )
		{

			is.getline( weapon, sizeof( weapon ) );

			is.getline( name, sizeof( name ) );

			is.getline( buttons, sizeof( buttons ) );

			is.getline( damage, sizeof( damage ) );
			int nDamage = atoi( damage );

			is.getline( active, sizeof( active ) );
			int nActive = atoi( active );

			if( nActive == 1 )
			{
				CCombo* combo = new CCombo( weapon, name , buttons, nDamage, true );
				m_vComboList.push_back( combo );
			}
			else
			{
				CCombo* combo = new CCombo( weapon, name , buttons, nDamage, false );
				m_vComboList.push_back( combo );
			}
		}
		is.close();
	}
}
// CheckComboInput
// Holds all the inputs from the player and checks if the condition for starting a combo and continuing a combo 
// are good.
int ComboSystem::CheckComboInput( char Buttons, string WeaponType, float fElapsedTime )
{
	if( Buttons )
	{
		switch( Buttons )
		{
		case 'z':
			{
				if( m_chBuffer[ 5 ]  != 0 )
				{
					Clear();
				}				
				for( int i = 0 ; i < 20 ; ++i )
				{
					if( m_chBuffer[ i ] == 0 )
					{
						m_chBuffer[ i ] = toupper( Buttons );
						m_chLastButton = Buttons;
						m_bTimerStart = true;
						break;
					}
					else
						continue;
				}		
				break;
			}
		case 'x':
			{
				if( m_chBuffer[ 5 ]  != 0 )
				{
					Clear();
				}
				for( int i = 0 ; i < 20 ; ++i )
				{
					if( m_chBuffer[ i ] == 0 )
					{
						m_chBuffer[ i ] = toupper( Buttons );
						m_chLastButton = Buttons;
						m_bTimerStart = true;
						break;
					}
					else
						continue;
				}		
				break;
			}
		case 'c':
			{
				if( m_chBuffer[ 5 ]  != 0 )
				{
					Clear();
				}
				for( int i = 0 ; i < 20 ; ++i )
				{
					if( m_chBuffer[ i ] == 0 )
					{
						m_chBuffer[ i ] = toupper( Buttons );
						m_chLastButton = Buttons;
						m_bTimerStart = true;
						break;
					}
					else
						continue;
				}	
				break;
			}
		case 'Z':
			{
				if( m_chBuffer[ 5 ]  != 0 )
				{
					Clear();
				}				
				for( int i = 0 ; i < 20 ; ++i )
				{
					if( m_chBuffer[ i ] == 0 )
					{
						m_chBuffer[ i ] = toupper( Buttons );
						m_chLastButton = Buttons;
						m_bTimerStart = true;
						break;
					}
					else
						continue;
				}		
				break;
			}
		case 'X':
			{
				if( m_chBuffer[ 5 ]  != 0 )
				{
					Clear();
				}				
				for( int i = 0 ; i < 20 ; ++i )
				{
					if( m_chBuffer[ i ] == 0 )
					{
						m_chBuffer[ i ] = toupper( Buttons );
						m_chLastButton = Buttons;
						m_bTimerStart = true;
						break;
					}
					else
						continue;
				}		
				break;
			}
		case 'C':
			{
				if( m_chBuffer[ 5 ]  != 0 )
				{
					Clear();
				}				
				for( int i = 0 ; i < 20 ; ++i )
				{
					if( m_chBuffer[ i ] == 0 )
					{
						m_chBuffer[ i ] = toupper( Buttons );
						m_chLastButton = Buttons;
						m_bTimerStart = true;
						break;
					}
					else
						continue;
				}		
				break;
			}
		default:
			return 0;
		};

	}
	for( unsigned int i = 0; i  < m_vComboList.size() ; ++i )
	{
		if( m_vComboList[ i ]->GetIsActive() )
		{
			if( strcmp( WeaponType.c_str() , m_vComboList[ i ]->GetWeaponName().c_str() ) == 0 )
			{
				if( m_fTimer <= 0.0f )
				{
					Clear();
					m_bTimerStart = false;
				}
				else
				{
					if( strlen( m_chBuffer ) >= 3 )
					{

						strcpy_s( m_chTemp, m_nCount + 1 , m_chBuffer );
						if( strcmp( m_chTemp, m_vComboList[ i ]->GetComboButtons().c_str() ) == 0 )
						{
							m_nCount = 3;
							m_bTimerStart = false;
							m_nCombo = m_vComboList[ i ]->GetComboBaseDamage();
							Clear();
							return i + 5;
						}

					}
				}
			}
		}
	}
	m_nCount = strlen( m_chBuffer ) + 1;
	if( strlen( m_chBuffer ) > 5 )
	{
		Clear();
		m_nCount = 3;
	}
	return 0;
}
// Update
// Updates the combo system during idle states.
void ComboSystem::Update( float fElapsedTime )
{
	if( m_bTimerStart )
		m_fTimer -= fElapsedTime ;
	else
		m_fTimer = 6.0f; 

}
// Clear
// Clears the combo buffer if good combo or been idle to long
void ComboSystem::Clear( void )
{
	for( int i = 0 ; i < 20 ; ++i )
		m_chBuffer[ i ] = 0;
	m_fTimer = 6.0f;
}
// ShutdownComboSystem
// When closing out the game play state will free up the memory storing the combo info.
void ComboSystem::ShutdownComboSystem( void )
{
	for( vector<CCombo*>::iterator iter = m_vComboList.begin() ; 
		iter != m_vComboList.end(); ++iter )
	{
		delete (*iter);
	}
	m_vComboList.clear();
}